package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.item.TbItemExample;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.pojo.itemCat.TbItemCatExample;
import com.dongdongshop.service.itemCat.ItemCatService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Service
@Component
public class ItemCatServiceImpl implements ItemCatService {

    @Resource
    private TbItemCatMapper itemCatMapper;

    @Resource
    private TbItemMapper itemMapper;

    /**
     * 根据商品分类的父级id字段获取分类信息
     */
    @Override
    public List<TbItemCat> getItemCatByParentId(Long pid) {
        TbItemCatExample example = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(pid);
        List<TbItemCat> itemCats = itemCatMapper.selectByExample(example);
        return itemCats;
    }

    /*
    * 增加商品分类数据
    * */
    @Override
    public void addItemCat(TbItemCat ic) {
        itemCatMapper.insertSelective(ic);
    }

    /*
    * 根据分类主键id查询信息
    * */
    @Override
    public TbItemCat getItemCatById(Long id) {
        return itemCatMapper.selectByPrimaryKey(id);
    }

    /*
    * 根据分类id修改分类信息
    * */
    @Override
    public void updateItemCatById(TbItemCat ic) {
        itemCatMapper.updateByPrimaryKeySelective(ic);
    }

    /*
    * 根据id批量删除商品分类数据 及该分类下面的子分类一起删除
    * */
    @Override
    public void deleteBatchItemCatByIds(Long[] ids) {
        //删除该id的数据
        itemCatMapper.deleteBatchItemCatByIds(ids);
        //删除父级id与传递过来的id相同的子分类
        for (Long id : ids) {
            TbItemCatExample example = new TbItemCatExample();
            TbItemCatExample.Criteria criteria = example.createCriteria();
            criteria.andParentIdEqualTo(id);
            itemCatMapper.deleteByExample(example);
        }
    }

    @Override
    public TbItemCat findItemcategory1Id(Long id) {
        return itemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbItemCat findItemcategory2Id(Long id) {
        return itemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbItemCat findItemcategory3Id(Long id) {
        return itemCatMapper.selectByPrimaryKey(id);
    }

}
